package metrics

import (
	"log"
	"math/rand/v2"
	"net"
	"strconv"
	"time"

	"github.com/prometheus/client_golang/prometheus"
)

var HistogramMetric = prometheus.NewHistogramVec(prometheus.HistogramOpts{
	Name:    "suber_example_histogram",           // 指标名称
	Help:    "一个 histogram 类型的指标.",               // 指标帮助信息
	Buckets: prometheus.LinearBuckets(0, 10, 10), // 设置桶宽度
}, []string{"selfIP"})

func Histogram() {
	timeStepInt, _ := strconv.Atoi(timeStep)
	addrs, _ := net.InterfaceAddrs()

	selfIP := ""
	for _, addr := range addrs {
		// 检查 IP 地址是否为回环地址
		if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
			if ipnet.IP.To4() != nil { // 优先返回 IPv4
				selfIP = ipnet.IP.String()
			}
		}
	}
	// 定期更新指标值
	go func() {
		for {
			a := rand.Float64() * 100
			log.Printf("histogram 随机浮点数 %v\n", a)
			HistogramMetric.WithLabelValues(selfIP).Observe(a)
			time.Sleep(time.Duration(timeStepInt) * time.Second)
		}
	}()

}
